package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;


    /**
     * 该接口必须登录才能访问
     * 将音视频文件上传腾讯云点播平台
     *
     * @param file 文件
     * @return {"mediaFileId":"音视频文件唯一标识","mediaUrl":"音视频文件播放地址"}
     */
    //@GuiGuLogin
    @Operation(summary = "将音视频文件上传腾讯云点播平台")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(MultipartFile file) {
        Map<String, String> map = trackInfoService.uploadTrack(file);
        return Result.ok(map);
    }


    /**
     * 该接口必须登录才能访问
     * 保存声音
     *
     * @param trackInfoVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "保存声音记录")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        //1.获取当前用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.保存声音
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }


    /**
     * 该接口必须登录才能访问
     * 查询当前用户拥有声音分页列表
     *
     * @param page
     * @param limit
     * @param trackInfoQuery
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "查询当前用户拥有声音分页列表")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> getUserTrackPage(@PathVariable int page,
                                                      @PathVariable int limit,
                                                      @RequestBody TrackInfoQuery trackInfoQuery) {
        //1.获取当前用户ID
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            userId = trackInfoQuery.getUserId();
        }
        trackInfoQuery.setUserId(userId);
        //2.构建分页所需Page对象 封装：页码+页大小
        Page<TrackListVo> pageInfo = new Page<>(page, limit);
        //3.调用业务逻辑完成条件分页查询 封装：总记录数，总页数，当前页数据
        pageInfo = trackInfoService.getUserTrackPage(pageInfo, trackInfoQuery);
        return Result.ok(pageInfo);
    }


    /**
     * 删除声音
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除声音")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }


    /**
     * 根据声音ID查询声音信息
     *
     * @param id
     * @return
     */
    @GuiGuCache(prefix = "track:info:")
    @Operation(summary = "根据声音ID查询声音信息")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }


    /**
     * 更新声音信息
     *
     * @param id
     * @param trackInfo
     * @return
     */
    @Operation(summary = "更新声音信息")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfo trackInfo) {
        trackInfoService.updateTrackInfo(trackInfo);
        return Result.ok();
    }


    /**
     * 分页展示专辑下声音列表-动态显示付费标识
     * 根据用户登录状态、身份、专辑付费类型、购买情况综合判断付费标识
     *
     * @param albumId
     * @param page
     * @param limit
     * @return 返回每一页声音列表（动态判断付费标识）
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "分页展示专辑下声音列表-动态显示付费标识")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> getAlbumTrackPage(@PathVariable Long albumId, @PathVariable int page, @PathVariable int limit) {
        //1.尝试获取用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.创建MP分页对象
        Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
        //3.调用业务层查询声音分页列表
        pageInfo = trackInfoService.getAlbumTrackPage(pageInfo, userId, albumId);
        return Result.ok(pageInfo);
    }


    /**
     * 根据选择待购声音作为购买起始声音得到分集购买列表
     *
     * @param trackId 选择付费标识声音-作为起始购买声音
     * @return [{name:"本集",price:1,trackCount:1},{name:"后10集",price:10,trackCount:10},{}.,{name:"全集",price:33,trackCount:33}]
     */
    @GuiGuLogin
    @Operation(summary = "根据选择待购声音作为购买起始声音得到分集购买列表")
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> getFenJiBuyList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> list = trackInfoService.getFenJiBuyList(userId, trackId);
        return Result.ok(list);
    }


    /**
     * 基于选择购买声音ID返回当前用户待结算声音列表
     * @param trackId
     * @param trackCount
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "基于选择购买声音ID返回当前用户待结算声音列表")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> getWaitBuyTrackInfoList(@PathVariable Long trackId, @PathVariable int trackCount){
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> list = trackInfoService.getWaitBuyTrackInfoList(userId, trackId, trackCount);
        return Result.ok(list);
    }
}

